﻿using System;
using System.Collections.Generic;

using Roslyn.Compilers.CSharp;

namespace ScriptXCompiler
{
    public class SyntaxTreeRelationship
    {
        private SyntaxTree _tree;
        public SyntaxTree Tree
        {
            get { return _tree; }
        }

        private List<NamedTypeSymbol> _provides;
        public IEnumerable<NamedTypeSymbol> Provides
        {
            get { return _provides; }
        }

        private List<TypeSymbol> _dependencies;
        public IEnumerable<TypeSymbol> Dependencies
        {
            get { return _dependencies; }
        }

        public SyntaxTreeRelationship(SyntaxTree tree)
        {
            _tree = tree;
            _provides = new List<NamedTypeSymbol>();
            _dependencies = new List<TypeSymbol>();
        }

        public void AddProvide(NamedTypeSymbol provide)
        {
            if (provide == null)
                throw new ArgumentNullException("provider");
            _provides.Add(provide);
        }

        public void AddDependency(TypeSymbol dependency)
        {
            if (dependency == null)
                throw new ArgumentNullException("dependency");
            _dependencies.Add(dependency);
        }

        public bool IsDependOn(SyntaxTreeRelationship that)
        {
            foreach (var d in this._dependencies)
            {
                foreach (var p in that._provides)
                {
                    if (d.ContainingNamespace == p.ContainingNamespace && d.Name == p.Name)
                        return true;
                }
            }
            return false;
        }
    }
}
